package king.dominic.jlibrary.view;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by king on 2016/5/12.
 * JViewPager
 */
@SuppressWarnings("unused")
public class JViewPager extends ViewPager {
    private static final String TAG = "JViewPager";
    private boolean isCanScroll = true;
    private int previousX;
    private int previousY;
    private boolean isClick;
    private DisplayMode mode = DisplayMode.DEFAULT;
    private boolean interceptTouch;

    public JViewPager(Context context) {
        super(context);
    }

    public JViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public JViewPager(Context context, AttributeSet attrs, int def) {
        super(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 是否能滑动
     *
     * @param isCanScroll 是否能滑动
     */
    public void setScrollable(boolean isCanScroll) {
        this.isCanScroll = isCanScroll;
    }

    @Override
    public void scrollTo(int x, int y) {
        super.scrollTo(x, y);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return isCanScroll && onJTouchEvent(event);
    }

    public void setDisplayMode(DisplayMode mode) {
        this.mode = mode;
    }

    public enum DisplayMode {
        DISPLAY_BY_FIRST_ONE, DISPLAY_BY_EVERY_ONE, DEFAULT
    }

    private boolean onJTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                isClick = true;
                break;

            case MotionEvent.ACTION_MOVE:
                isClick = false;
                break;
            case MotionEvent.ACTION_UP:
                if (isClick)
                    onSingleTouch();
                break;
        }
        return super.onTouchEvent(event);
    }


    /**
     * 单击
     */
    public void onSingleTouch() {
        if (onSingleTouchListener != null) {
            onSingleTouchListener.onSingleTouch();
        }
    }

    /**
     * 单击事件监听器
     */
    public interface OnSingleTouchListener {
        void onSingleTouch();
    }

    private OnSingleTouchListener onSingleTouchListener;

    /**
     * 设置单击监听器
     *
     * @param onSingleTouchListener 设置单击监听器
     */
    public void setOnSingleTouchListener(OnSingleTouchListener onSingleTouchListener) {
        this.onSingleTouchListener = onSingleTouchListener;
    }

    @Override
    public void setCurrentItem(int item, boolean smoothScroll) {
        super.setCurrentItem(item, smoothScroll);
    }

    @Override
    public void setCurrentItem(int item) {
        super.setCurrentItem(item);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return isCanScroll && onJInterceptTouchEvent(ev);
    }

    private boolean onJInterceptTouchEvent(MotionEvent event) {
        if (!super.onInterceptTouchEvent(event)) return false;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                interceptTouch = false;
                previousX = (int) event.getX();
                previousY = (int) event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                int currentX = (int) event.getX();
                int currentY = (int) event.getY();
                int absX = Math.abs(currentX - previousX);
                int absY = Math.abs(currentY - previousY);
                if (absX > absY) {
                    interceptTouch = true;
                }
                previousX = currentX;
                previousY = currentY;
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return interceptTouch;
    }

    private boolean hasFocus = true;

    public void setHasFocus(boolean hasFocus) {
        this.hasFocus = hasFocus;
    }

    @Override
    public boolean hasFocus() {
        return hasFocus && super.hasFocus();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        switch (mode) {
            case DISPLAY_BY_EVERY_ONE:
                onRemeasureEveryOne(widthMeasureSpec, heightMeasureSpec);
                break;
            case DISPLAY_BY_FIRST_ONE:
//                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//                getChildAt(0).measure(widthMeasureSpec,heightMeasureSpec);
//                setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
//                break;
            case DEFAULT:
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                break;
        }
    }

    private int measureWidth(int widthMeasureSpec, View v) {
        int result = 0;
        int measureMode = MeasureSpec.getMode(widthMeasureSpec);
        int measureSize = MeasureSpec.getSize(widthMeasureSpec);
        if (measureMode == MeasureSpec.EXACTLY)
            return measureSize;
        if (v != null)
            result = v.getMeasuredWidth();
        if (measureMode == MeasureSpec.AT_MOST)
            result = Math.min(result, measureSize);
        return result;
    }

    private int measureHeight(int heightMeasureSpec, View v) {
        int result = 0;
        int measureMode = MeasureSpec.getMode(heightMeasureSpec);
        int measureSize = MeasureSpec.getSize(heightMeasureSpec);
        if (measureMode == MeasureSpec.EXACTLY)
            return measureSize;
        if (v != null)
            result = v.getMeasuredHeight();
        if (measureMode == MeasureSpec.AT_MOST)
            result = Math.min(result, measureSize);
        return result;
    }

    private void onRemeasureEveryOne(int widthMeasureSpec, int heightMeasureSpec) {
        if (getAdapter() == null)
            return;
        Object object = getAdapter().instantiateItem(this, getCurrentItem());
        View view = null;
        if (object instanceof Fragment)
            view = ((Fragment) object).getView();
        if (object instanceof View)
            view = (View) object;

        if (view != null) {
            int i = getChildCount();
            for (int ii = 0; ii < i; ii++) {
                View v = getChildAt(ii);
                if (v != null)
                    v.measure(widthMeasureSpec, heightMeasureSpec);
            }
        }
        setMeasuredDimension(measureWidth(widthMeasureSpec, view), measureHeight(heightMeasureSpec, view));
    }

}
